﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FrogEngine;

namespace TestWPFApp
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();

			tree = new RedBlackTree<double, string>();
			tree.InsertItem(4, "H");
			tree.InsertItem(7, "E");
			tree.InsertItem(12, "L");
			tree.InsertItem(15, "L");
			tree.InsertItem(3, "O");
			tree.InsertItem(5, "W");
			tree.InsertItem(14, "O");
			tree.InsertItem(18, "R");
			tree.InsertItem(16, "L");
			tree.InsertItem(17, "D");
			
		}

		RedBlackTree<double, string> tree;

		public void DrawTree(RedBlackTree<double, string> tree)
		{
			canvas.Children.Clear();
			int count = 0;
			tree.InOrderTraversal(tree.Root, new Action<IRedBlackTreeNode<double, string>>(o => 
			{
				int size = tree.Size;
				int nodeHeight = tree.Height(o);
				int totalHeight = tree.Height(tree.Root);

				double cWidth = 655;
				double cHeight = 375;

				double dx = cWidth / size;
				double dy = cHeight / totalHeight;

				Ellipse e = new Ellipse() { Width = 30, Height = 30, Fill = o.Color == TreeColor.Red ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Black) };
				TextBlock b = new TextBlock() { Text = o.Key.ToString(), Foreground = new SolidColorBrush(Colors.White) };
				canvas.Children.Add(e);
				canvas.Children.Add(b);
				e.SetValue(LeftProperty, count * dx);
				e.SetValue(TopProperty, cHeight - (nodeHeight * dy));
				b.SetValue(LeftProperty, count * dx + 5);
				b.SetValue(TopProperty, cHeight - (nodeHeight * dy) + 5);
				count++;
			}));
		}

		private void Button_Click(object sender, RoutedEventArgs e)
		{
			DrawTree(tree);
		}

		private void Button_Click_1(object sender, RoutedEventArgs e)
		{
			double val = double.Parse(text.Text);
			tree.RemoveItem(val);
			DrawTree(tree);
		}

		private void Button_Click_2(object sender, RoutedEventArgs e)
		{
			double key = double.Parse(text.Text);
			tree.InsertItem(key, string.Empty);
			DrawTree(tree);
		}
	}
}
